package pri.lt.client;

import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import pri.lt.Constants;
import pri.lt.bean.BBSResponse;

import java.util.List;
import java.util.function.Consumer;

public class UserClient {


    public static String jsessionid = null;

    public static void login() {
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
//  //请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//  //封装参数，千万不要替换为Map与HashMap，否则参数无法传递
        MultiValueMap<String, String> params2= new LinkedMultiValueMap<String, String>();
//  //也支持中文
        params2.add("username", "lhr");
        params2.add("password", "12345678admin");
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params2, headers);


        ResponseEntity<BBSResponse> loginResult = restTemplate.exchange(Constants.BBS_ADMIN_LOGIN, HttpMethod.POST, requestEntity, BBSResponse.class, params2);
        List<String> setCookies = loginResult.getHeaders().get("Set-Cookie");
        for(String cookie : setCookies) {
            if (cookie.contains("JSESSIONID")) {
                String setJsessionId = cookie.split(";")[0];
                jsessionid = setJsessionId.split("=")[1];
            }
        }


    }

}
